package com.nineclock.doc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.doc.dto.DocFileDto;
import com.nineclock.doc.mapper.DocFileMapper;
import com.nineclock.doc.pojo.DocFile;
import com.nineclock.doc.service.PageResultService;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.feign.SysUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：zhouc
 * @date ：Created in 2021/6/28
 * @description ：
 * @version: 1.0
 */
@Service
public class PageResultServiceImpl implements PageResultService {

    @Autowired
    DocFileMapper docFileMapper;
    @Autowired
    SysUserFeign sysUserFeign;

    @Override
    public PageResult pageResults(Long page, Long pageSize, String keyword) {
        //获取线程中的企业Id
        Long companyId = CurrentUserHolder.get().getCompanyId();
        Page<DocFile> docFilePage = new Page<>(page, pageSize);
        //查询所有的文件名称
        LambdaQueryWrapper<DocFile> docFileLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(keyword!=null) {
            docFileLambdaQueryWrapper.and(wrapper -> {
                wrapper.like(DocFile::getContent, keyword).or().like(DocFile::getName, keyword).or()
                       .like(DocFile::getFilePath,keyword);
            });
        }
        docFileLambdaQueryWrapper.eq(DocFile::getCompanyId, companyId);
        docFilePage = docFileMapper.selectPage(docFilePage, docFileLambdaQueryWrapper);
        
        PageResult<DocFileDto> objectPageResult = new PageResult<>();
        //遍历所有的文件
        List<DocFile> records = docFilePage.getRecords();
        if (CollectionUtils.isNotEmpty(records)) {
            List<DocFileDto> docFileDtos = BeanHelper.copyWithCollection(records, DocFileDto.class);
            for (DocFileDto fileDto : docFileDtos) {
                List<SysCompanyUserDTO> collaborations2 = new ArrayList<>();
                Long createUserId = fileDto.getCreateUserId();
                SysCompanyUserDTO companyUserDTO =
                        sysUserFeign.findCompanyUserByCreateUserId(createUserId).getData();
                collaborations2.add(companyUserDTO);
                if (companyUserDTO != null) {
                    fileDto.setCreator(companyUserDTO);
                    fileDto.setCollaborations(collaborations2);
                } else {
                    fileDto.setCollaborations(new ArrayList<>());
                }


            }

            return new PageResult(docFilePage.getTotal(), docFilePage.getPages(), docFileDtos);
        }


        return null;
    }
}
